package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.enums.UserReadEnum;
import com.settlement.system.mapper.SysLazyToolUserMapper;
import com.settlement.system.mapper.SysLazyUserNoticeMapper;
import com.settlement.system.model.entity.SysLazyToolUser;
import com.settlement.system.model.entity.SysLazyUserNotice;
import com.settlement.system.model.form.SysLazyUserNoticeForm;
import com.settlement.system.model.query.SysLazyUserNoticeQuery;
import com.settlement.system.model.vo.SysLazyUserNoticeImportVo;
import com.settlement.system.service.SysLazyUserNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户通知表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-01-05
 */
@Service
@RequiredArgsConstructor
public class SysLazyUserNoticeServiceImpl extends ServiceImpl<SysLazyUserNoticeMapper, SysLazyUserNotice> implements SysLazyUserNoticeService {

    private final SysLazyToolUserMapper sysLazyToolUserMapper;

    @Override
    public Page<SysLazyUserNotice> getPage(SysLazyUserNoticeQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysLazyUserNotice> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysLazyUserNotice(SysLazyUserNoticeForm sysLazyUserNoticeForm) {
        SysLazyUserNotice sysLazyUserNotice = new SysLazyUserNotice();
        BeanUtil.copyProperties(sysLazyUserNoticeForm, sysLazyUserNotice);
        sysLazyUserNotice.setCreateTime(null);
        return this.saveOrUpdate(sysLazyUserNotice);
    }

    @Override
    public boolean saveSysLazyUserNotice(SysLazyUserNotice sysLazyUserNotice) {
        SysLazyToolUser sysLazyToolUser = sysLazyToolUserMapper.selectById(sysLazyUserNotice.getUserId());
        sysLazyUserNotice.setMobile(sysLazyToolUser.getMobile());
        return this.saveOrUpdate(sysLazyUserNotice);
    }

    @Override
    public SysLazyUserNoticeForm getSysLazyUserNoticeFormData(Long id) {
        SysLazyUserNotice sysLazyUserNotice = this.getById(id);
        SysLazyUserNoticeForm sysLazyUserNoticeForm = new SysLazyUserNoticeForm();
        BeanUtil.copyProperties(sysLazyUserNotice, sysLazyUserNoticeForm);
        return sysLazyUserNoticeForm;
    }

    @Override
    public boolean deleteSysLazyUserNotice(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysLazyUserNoticeImportVo> voList) {
        List<SysLazyUserNotice> sysLazyUserNoticeList = new ArrayList<>();
        for (SysLazyUserNoticeImportVo importVo : voList) {
            SysLazyUserNotice sysLazyUserNotice = new SysLazyUserNotice();
            BeanUtil.copyProperties(importVo, sysLazyUserNotice);
            sysLazyUserNoticeList.add(sysLazyUserNotice);
        }
        this.saveOrUpdateBatch(sysLazyUserNoticeList);
    }

    @Override
    public List<SysLazyUserNoticeImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<SysLazyUserNoticeImportVo>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysLazyUserNotice> sysLazyUserNoticeList = this.getBaseMapper().selectBatchIds(ids);
        List<SysLazyUserNoticeImportVo> voList = new ArrayList<>();
        for (SysLazyUserNotice sysLazyUserNotice : sysLazyUserNoticeList) {
            SysLazyUserNoticeImportVo vo = new SysLazyUserNoticeImportVo();
            BeanUtil.copyProperties(sysLazyUserNotice, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void readNotice(Long noticeId) {
        SysLazyUserNotice sysLazyUserNotice = this.baseMapper.selectById(noticeId);
        sysLazyUserNotice.setUserRead(UserReadEnum.Y.getValue());
        this.baseMapper.updateById(sysLazyUserNotice);
    }

    @Override
    public long countUserUnRead(Long userId) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<SysLazyUserNotice>().eq(SysLazyUserNotice::getUserId, userId).eq(SysLazyUserNotice::getUserRead, UserReadEnum.N.getValue()));
    }
}
